package tt.dz.egobus.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.common.CommonApiService;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.bus.BusApiService;
import tt.dz.egobus.api.managerservice.bus.BusVo;
import tt.dz.egobus.bean.CompareOpEnum;
import tt.dz.egobus.servicemanager.entities.Bus;
import tt.dz.egobus.servicemanager.service.BusService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.api.util.ResponseHelper.copy;
import static tt.dz.egobus.bean.I18NCodeEnum.RPC_PARAM_ENTITY_ERROR;
import static tt.dz.egobus.bean.I18NCodeEnum.RPC_PARAM_ILLEGAL_ERROR;

/**
 * @author Anson
 * @date 17-3-16
 * @name
 * @since 1.0.1
 */
@Service(version = "1.0.1", interfaceClass = BusApiService.class)
public class BusApiServiceImpl extends CommonApiService implements BusApiService {
    public static final String BUS_SERVICE_SERVICE_NAME = "busServiceImpl";

    @Override
    public ResponseBack<BusVo> queryInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusService.class);
        Bus entity = service.get(id);
        if (entity==null) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusVo response = copy(entity, new BusVo());
        return ResponseBack.success(response);
    }

    @Override
    public ResponseBack<List<BusVo>> queryInfos(String[] ids) throws ResponseException {

        if (null == ids || ids.length == 0) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusService.class);
        List<Bus> entities = service.queryByIds(ids);
        List<BusVo> response = copy(entities, BusVo.class);
        return ResponseBack.success(response);
    }

    @Override
    public ResponseBack<List<BusVo>> queryStatus(int status) throws ResponseException {
        if (status < -100) {
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        return this.queryBus0(null, CompareOpEnum.EQ, status);
    }

    @Override
    public ResponseBack<List<BusVo>> querySeatNum(int seatNum) {
        return this.querySeatNum(seatNum, CompareOpEnum.EQ);
    }

    @Override
    public ResponseBack<List<BusVo>> querySeatNum(int seatNum, CompareOpEnum compare) {
        return this.queryBus0(seatNum, compare, null);
    }

    @Override
    public ResponseBack<List<BusVo>> querySeatNumStatus(int seatNum, int status) throws ResponseException {
        return this.queryBus0(seatNum, CompareOpEnum.EQ, status);
    }


    @Override
    public ResponseBack<List<BusVo>> querySeatNumStatus(int seatNum, CompareOpEnum compare, int status)
            throws ResponseException {
        return this.queryBus0(seatNum, compare, status);
    }

    @Override
    public ResponseBack<Boolean> update(BusVo busVo) throws ResponseException {
        if(busVo==null || StringUtils.isEmpty(busVo.getId())){
            return ResponseBack.error(this.get(RPC_PARAM_ILLEGAL_ERROR));
        }
        BusService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusService.class);
        Bus bus = new Bus();
        bus = copy(busVo,bus);
        try {
            service.update(bus);
        }catch (Exception e){
            throw new ResponseException(this.get(RPC_PARAM_ENTITY_ERROR));
        }
        return ResponseBack.success(true);
    }

    /**
     *
     * @param seatNum
     * @param compare
     * @param status
     * @return 保存的和新方法
     * @throws ResponseException
     */
    private ResponseBack<List<BusVo>> queryBus0(Integer seatNum, CompareOpEnum compare, Integer status)
            throws ResponseException {
        Map<String, Object> params = new HashMap<String, Object>(2);
        if (null != status) {
            params.put("eq#status", status);
        }
        if (null != seatNum) {
            compare = compare == null ? CompareOpEnum.EQ : compare;
            params.put(compare.getMsg() + "#" + Bus.BUS_TYPE_ALIAS.getK() + ".seatNum", seatNum.intValue());
        }
        BusService service = this.getService(BUS_SERVICE_SERVICE_NAME, BusService.class);
        List<Bus> entities = service.list(params);
        List<BusVo> response = copy(entities, BusVo.class);
        return ResponseBack.success(response);
    }
}
